package com.xdw.permissionslib.mylibrary;

import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.bundle.IBundleManager;

import java.util.ArrayList;
import java.util.List;

public class StrongPermissionsUtils {
    private static final String TAG = StrongPermissionsUtils.class.getName();
    //定义权限请求识别码
    private static final int PERMISSION_REQUEST_CODE = 1;
    //定义实例
    private static StrongPermissionsUtils strongPermissionsUtils;
    //定义鉴权状态监听接口实例
    private PermissionStateListener permissionStateListener;
    //定义context实例
    private static AbilityContext abilityContext;
    //标识用户是否勾选了"禁止XX权限，以后不再提示"，默认false，用户未勾选
    private boolean isDisabledPrompt = false;

    private StrongPermissionsUtils() {
    }

    /**
     * 获取StrongPermissionsUtils单例
     *
     * @return StrongPermissionsUtils
     */
    public static synchronized StrongPermissionsUtils getInstance(AbilityContext context) {
        if (strongPermissionsUtils == null) {
            strongPermissionsUtils = new StrongPermissionsUtils();
        }
        abilityContext = context;
        return strongPermissionsUtils;
    }


    /**
     * checkPermissions
     *
     * @param context     context
     * @param permissions 需要申请的权限集合
     */
    public StrongPermissionsUtils checkPermissions(Context context, String[] permissions) {
        List<String> applyPermissions = new ArrayList<>();

        for (String element : permissions) {
            if (context.verifySelfPermission(element) != IBundleManager.PERMISSION_GRANTED) {
                if (context.canRequestPermission(element)) {
                    LogUtil.warn(TAG, "需要申请权限:" + element);
                    applyPermissions.add(element);
                } else {
                    //禁止授权还不提示的情形
                    LogUtil.warn(TAG, "禁止授权并且系统不再提示:" + element);
                    isDisabledPrompt = true;    //永久禁止了授权，需要手动进入系统应用权限管理页面开启
                    //执行权限拒绝之后的回调
                    permissionStateListener.onPermissionDenied(isDisabledPrompt);
                    return strongPermissionsUtils;
                }
            }
        }

        if (!isDisabledPrompt && !applyPermissions.isEmpty()) {
            //主动申请授权，会自动触发系统弹出提供给用户确认
            context.requestPermissionsFromUser(applyPermissions.toArray(new String[0]), PERMISSION_REQUEST_CODE);
        } else {
            //所有权限校验通过，不存在未进行授权的权限，此时触发获取到所有授权的回调，以便执行获得授权之后的业务逻辑
            permissionStateListener.onPermissionGranted();
        }
        return strongPermissionsUtils;

    }

    public void setPermissionStateListener(PermissionStateListener permissionStateListener) {
        this.permissionStateListener = permissionStateListener;
    }

    /**
     * 在需要鉴权的Ability中的onRequestPermissionsFromUserResult方法调用此方法
     * 该方法必须调用，否则无法之前鉴权通过之后的回调
     *
     * @param requestCode  requestCode
     * @param permissions  permissions
     * @param grantResults grantResults
     */
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) {
            return;
        }
        for (int grantResult : grantResults) {
            if (grantResult != IBundleManager.PERMISSION_GRANTED) {
                //执行权限拒绝之后的回调
                permissionStateListener.onPermissionDenied(isDisabledPrompt);
                return;
            }
        }
        //执行权限授予之后的回调
        permissionStateListener.onPermissionGranted();
    }

    /**
     * 定义一个回调接口，用于监听权限申请结果的回调
     */
    public interface PermissionStateListener {
        /**
         * 当授权时
         */
        void onPermissionGranted();

        /**
         * 当拒绝授权时触发
         */
        void onPermissionDenied(boolean isDisabledPrompt);
    }


}
